package me.exchange.com.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.util.*;

@Component
public class PlanOrderUtils {

    @Autowired
    private JsonUtilJackson jsonUtilJackson;


    private static final String url="http://180.76.246.175:9999/tripapi/flight/";

    private static final String api_key="10030";


    private static final String api_secret="60e255c45c9021ee55d8ffa6b3cd8306";


    /**
     * 取消订单
     * @param params
     * @return
     */
      public static JSONObject cancelOrder(Map<String,Object> params){
          Map<String,Object>map=new HashMap<>();
          JsonUtilJackson jsonUtilJackson=new JsonUtilJackson();
          map.put("apiKey",api_key);
          map.put("timestamp", DateUtil.getDateFormate(new Date()));
          map.put("sign",sign(map,params));
          map.put("data",params);
          return new JSONObject(HttpUtil.post(url+"cancelorder",jsonUtilJackson.toJson(map)));
}


    /**
     * 出票确认
     * @param params
     * @return
     */
    public static JSONObject confirmOrder(Map<String,Object> params){
        Map<String,Object>map=new HashMap<>();
        JsonUtilJackson jsonUtilJackson=new JsonUtilJackson();
        map.put("apiKey",api_key);
        map.put("timestamp", DateUtil.getDateFormate(new Date()));
        map.put("sign",sign(map,params));
        map.put("data",params);
        return new JSONObject(HttpUtil.post(url+"confirmorder",jsonUtilJackson.toJson(map)));
    }

    /**
     * 签名规则
     * @param map1
     * @param map2
     * @return
     */
    public static String sign(Map<String,Object>map1,Map<String,Object>map2){
        // 合并
        Map<String, Object> map = new HashMap<>();
        map.putAll(map1);
        map.putAll(map2);
        Set<String> keySet = map.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (map.get(k)!=null && map.get(k).toString().trim().length() > 0)
                // 参数值为空，则不参与签名
                sb.append(k).append("=").append(map.get(k).toString().trim()).append("&");
        }
        sb.append(api_secret);
        sb.insert(0,api_secret+"&");
         try {
                return MD5(sb.toString()).toUpperCase();
            } catch (Exception e) {
                e.printStackTrace();
            }
        return "";
    }

    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

}
